<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>fpsdisplay.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_debug_fpsdisplay.js.html">fpsdisplay.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2011 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Displays frames per second (FPS) for the current window.
<a name="line17"></a> * Only supported in browsers that support requestAnimationFrame.
<a name="line18"></a> * See: https://developer.mozilla.org/en/DOM/window.requestAnimationFrame.
<a name="line19"></a> *
<a name="line20"></a> * @see ../demos/fpsdisplay.html
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.debug.FpsDisplay&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line26"></a>goog.require(&#39;goog.async.AnimationDelay&#39;);
<a name="line27"></a>goog.require(&#39;goog.ui.Component&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Displays frames per seconds that the window this component is
<a name="line33"></a> * rendered in is animating at.
<a name="line34"></a> *
<a name="line35"></a> * @param {goog.dom.DomHelper=} opt_domHelper An optional dom helper.
<a name="line36"></a> * @constructor
<a name="line37"></a> * @extends {goog.ui.Component}
<a name="line38"></a> */
<a name="line39"></a>goog.debug.FpsDisplay = function(opt_domHelper) {
<a name="line40"></a>  goog.base(this, opt_domHelper);
<a name="line41"></a>};
<a name="line42"></a>goog.inherits(goog.debug.FpsDisplay, goog.ui.Component);
<a name="line43"></a>
<a name="line44"></a>
<a name="line45"></a>/**
<a name="line46"></a> * CSS class for the FPS display.
<a name="line47"></a> */
<a name="line48"></a>goog.debug.FpsDisplay.CSS = goog.getCssName(&#39;goog-fps-display&#39;);
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>/**
<a name="line52"></a> * The number of samples per FPS report.
<a name="line53"></a> */
<a name="line54"></a>goog.debug.FpsDisplay.SAMPLES = 10;
<a name="line55"></a>
<a name="line56"></a>
<a name="line57"></a>/**
<a name="line58"></a> * The current animation.
<a name="line59"></a> * @type {goog.debug.FpsDisplay.FpsAnimation_}
<a name="line60"></a> * @private
<a name="line61"></a> */
<a name="line62"></a>goog.debug.FpsDisplay.prototype.animation_ = null;
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/** @override */
<a name="line66"></a>goog.debug.FpsDisplay.prototype.createDom = function() {
<a name="line67"></a>  this.setElementInternal(
<a name="line68"></a>      this.getDomHelper().createDom(&#39;div&#39;, goog.debug.FpsDisplay.CSS));
<a name="line69"></a>};
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/** @override */
<a name="line73"></a>goog.debug.FpsDisplay.prototype.enterDocument = function() {
<a name="line74"></a>  goog.base(this, &#39;enterDocument&#39;);
<a name="line75"></a>  this.animation_ = new goog.debug.FpsDisplay.FpsAnimation_(this.getElement());
<a name="line76"></a>  this.delay_ = new goog.async.AnimationDelay(
<a name="line77"></a>      this.handleDelay_, this.getDomHelper().getWindow(), this);
<a name="line78"></a>  this.delay_.start();
<a name="line79"></a>};
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * @param {number} now The current time.
<a name="line84"></a> * @private
<a name="line85"></a> */
<a name="line86"></a>goog.debug.FpsDisplay.prototype.handleDelay_ = function(now) {
<a name="line87"></a>  if (this.isInDocument()) {
<a name="line88"></a>    this.animation_.onAnimationFrame(now);
<a name="line89"></a>    this.delay_.start();
<a name="line90"></a>  }
<a name="line91"></a>};
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/** @override */
<a name="line95"></a>goog.debug.FpsDisplay.prototype.exitDocument = function() {
<a name="line96"></a>  goog.base(this, &#39;exitDocument&#39;);
<a name="line97"></a>  this.animation_ = null;
<a name="line98"></a>  goog.dispose(this.delay_);
<a name="line99"></a>};
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * @return {number} The average frames per second.
<a name="line104"></a> */
<a name="line105"></a>goog.debug.FpsDisplay.prototype.getFps = function() {
<a name="line106"></a>  goog.asserts.assert(
<a name="line107"></a>      this.isInDocument(), &#39;Render the FPS display before querying FPS&#39;);
<a name="line108"></a>  return this.animation_.lastFps_;
<a name="line109"></a>};
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * @param {Element} elem An element to hold the FPS count.
<a name="line115"></a> * @constructor
<a name="line116"></a> * @private
<a name="line117"></a> */
<a name="line118"></a>goog.debug.FpsDisplay.FpsAnimation_ = function(elem) {
<a name="line119"></a>  /**
<a name="line120"></a>   * An element to hold the current FPS rate.
<a name="line121"></a>   * @type {Element}
<a name="line122"></a>   * @private
<a name="line123"></a>   */
<a name="line124"></a>  this.element_ = elem;
<a name="line125"></a>
<a name="line126"></a>  /**
<a name="line127"></a>   * The number of frames observed so far.
<a name="line128"></a>   * @type {number}
<a name="line129"></a>   * @private
<a name="line130"></a>   */
<a name="line131"></a>  this.frameNumber_ = 0;
<a name="line132"></a>};
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * The last time which we reported FPS at.
<a name="line137"></a> * @type {number}
<a name="line138"></a> * @private
<a name="line139"></a> */
<a name="line140"></a>goog.debug.FpsDisplay.FpsAnimation_.prototype.lastTime_ = 0;
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>/**
<a name="line144"></a> * The last average FPS.
<a name="line145"></a> * @type {number}
<a name="line146"></a> * @private
<a name="line147"></a> */
<a name="line148"></a>goog.debug.FpsDisplay.FpsAnimation_.prototype.lastFps_ = -1;
<a name="line149"></a>
<a name="line150"></a>
<a name="line151"></a>/**
<a name="line152"></a> * @param {number} now The current time.
<a name="line153"></a> */
<a name="line154"></a>goog.debug.FpsDisplay.FpsAnimation_.prototype.onAnimationFrame = function(now) {
<a name="line155"></a>  var SAMPLES = goog.debug.FpsDisplay.SAMPLES;
<a name="line156"></a>  if (this.frameNumber_ % SAMPLES == 0) {
<a name="line157"></a>    this.lastFps_ = Math.round((1000 * SAMPLES) / (now - this.lastTime_));
<a name="line158"></a>    this.element_.innerHTML = this.lastFps_;
<a name="line159"></a>    this.lastTime_ = now;
<a name="line160"></a>  }
<a name="line161"></a>  this.frameNumber_++;
<a name="line162"></a>};
</pre>


</body>
</html>
